package com.shouhe.mysap.controller.pur;


import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.common.status.PlanStatus;
import com.shouhe.mysap.common.status.PurStatus;
import com.shouhe.mysap.entity.PlanNoSupplier;
import com.shouhe.mysap.entity.PurAskPrice;
import com.shouhe.mysap.entity.PurPerson;
import com.shouhe.mysap.entity.PurPrice;
import com.shouhe.mysap.service.PlanNoSupplierService;
import com.shouhe.mysap.service.PurAskPriceService;
import com.shouhe.mysap.service.PurPersonService;
import com.shouhe.mysap.service.PurPriceService;
import com.shouhe.mysap.util.AddUtil;
import com.shouhe.mysap.util.PageResultUtil;
import com.shouhe.mysap.util.SelectUtil;
import com.shouhe.mysap.util.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 守鹤
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/pur-price")
public class PurPriceController {

    @Autowired
    private PurPriceService purPriceService;
    @Autowired
    private PlanNoSupplierService planNoSupplierService;
    @Autowired
    private PurAskPriceService purAskPriceService;
    @Autowired
    private PurPersonService purPersonService;


    /**
     * @描述 得到全部的采购人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPurPrices")
    public PageResult getPurPrices(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        PageResult pageResult = PageResultUtil.getPageResult(page, rows, purPriceService);
        return pageResult;
    }





    @RequestMapping("/addPurPrice")
    public MySapResult addPurPrice(PurPrice price) {

        Integer priceAskPriceId = price.getId();
        PurAskPrice purAskPrice = new PurAskPrice().setId(priceAskPriceId).setStatus(PurStatus.PUR_ASK_PRICE_HAS_SUPPLIER);
        MySapResult result = UpdateUtil.updateObjectById("更新询价报表物料信息为有供应商", purAskPrice, purAskPriceService);
        if (!result.isSuccess()) {
            return result;
        }

        price.setStatus(PurStatus.PUR_PRICE_FINANCE_NOCHECK);
        Integer purGroup = price.getPurGroup();
        PurPerson purPerson = SelectUtil.selectOneEq("pur_group", purGroup, purPersonService);
        price.setPurName(purPerson.getPurName());
        MySapResult result2 = AddUtil.addObject("在采购价格表中添加一条记录", price, purPriceService);
        if(!result2.isSuccess()){
            return  result2;
        }

        //当是主供应商时才改变计划没供应商记录的状态
        if(price.getMainSupplier()!=null&&price.getMainSupplier().equals("1")){
            PlanNoSupplier noSupplier = SelectUtil.selectOneEq("thing_code", price.getThingCode(), planNoSupplierService);
            //财务未审核
            noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_FINANCE_NOCHECK);
            noSupplier.setSupplierCode(price.getSupplierCode());
            noSupplier.setSupplierName(price.getSupplierName());
            MySapResult result1 = UpdateUtil.updateObjectById("更新计划没有供应商表数据的状态为，财务为审核价格", noSupplier, planNoSupplierService);
            if(!result1.isSuccess()){
                return result1;
            }
        }
        return MySapResult.success();
    }


}

